<?php
class cls_control_user extends cls_control_base
{
	function show()
	{
		if (!empty($_SESSION['admin']['user_name']))
		{
			redirection('index');
		}
		$this->smarty->display('user/login.htm');
	}
	
	function login()
	{
		$user_name = trim(strtolower($_REQUEST['user_name']));
		$password = trim(strtolower($_REQUEST['password']));
		
		$user_serve = parent::get_serve();
		$result = $user_serve->login($user_name, $password);
		if ($result['success']) {
			redirection('index');
		} else {
			$this->smarty->assign('user_name', $user_name);
			$this->smarty->assign('password', $password);
			$this->smarty->assign('msg', $result['msg']);
			$this->show();
		}
	}
	
	function logout()
	{
		$user_serve = parent::get_serve();
		$user_serve->logout();
		redirection();
	}

	function myinfo()
	{
		$user_serve = parent::get_serve();
		$myinfo = $user_serve->user_info($_SESSION['admin']['user_name']);
		$this->smarty->assign('info', $myinfo);
		$this->smarty->assign('edit', true);
		$this->smarty->display('user/user_info.htm');
	}
	
	function user_list_show()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('ADMIN_MANAGE','PERMISSION_ALLOCATION');
		$user_list = parent::serve_action('get_user_list');
		$this->smarty->assign('user_list', $user_list);
		$this->smarty->display('user/user_list.htm');
	}
	
	function update()
	{
		$parameter = array(
			'user_name' => $_SESSION['admin']['user_name'],
			'old_password' => trim(strtolower($_POST['old_password'])),
			'password' => trim(strtolower($_POST['new_password'])),
			'password_confirm' => trim(strtolower($_POST['new_password_confirm'])),
			'nick_name' => trim($_POST['nick_name']),
			'real_name' => trim($_POST['real_name']),
		);
		
		$user_serve = parent::get_serve();
		$result = $user_serve->update($parameter);
		if (!$result['success']) {
			echo $result['msg'];
		} else {
			echo "修改成功";
		}
	}
	
	function create_show()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('ADMIN_MANAGE');
		$this->smarty->display('user/create_show.htm');
	}
	
	function create()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('ADMIN_MANAGE');
		$parameter = array(
				'user_name' => $_POST['new_user_name'],
				'password' => trim(strtolower($_POST['new_password'])),
				'password_confirm' => trim(strtolower($_POST['new_password_confirm'])),
				'nick_name' => trim($_POST['nick_name']),
				'real_name' => trim($_POST['real_name']),
		);
	
		$user_serve = parent::get_serve();
		$result = $user_serve->create($parameter);
		if (!$result['success']) {
			echo $result['msg'];
		} else {
			echo "创建成功";
		}
	}
	
	function info_show()
	{
		$user_name = $_GET['user_name'];
		$user_serve = parent::get_serve();
		$user_info = $user_serve->user_info($user_name);
		$this->smarty->assign('info', $user_info);
		$this->smarty->assign('edit', false);
		$this->smarty->display('user/user_info.htm');
	}
	
	function reset_password()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('ADMIN_MANAGE');
		$user_name = $_GET['user_name'];
		$serve = parent::get_serve();
		echo "成功重置密码";
	}
}